package com.clh.controller;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.clh.dto.Result;
import com.clh.entity.Admin;
import com.clh.service.AdminService;
import com.clh.service.impl.AdminServiceImpl;

/**
 * Servlet implementation class AdminServlet
 */
@WebServlet("/AdminServlet")
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private AdminService adminService = new AdminServiceImpl();
	private Admin admin = new Admin();

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String op = request.getParameter("op");
		if ("adminLogin".equals(op)) {
			doAdminLogin(request, response);
		} else if ("adminLogout".equals(op)) {
			doAdminLogout(request, response);
		}
	}

	private void doAdminLogin(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/html;charset=utf-8");

		// Result result = new Result();

		try {
			String adminName = request.getParameter("adminName");
			String adminPassword = request.getParameter("adminPassword");
			String remember = request.getParameter("remember");

//			System.out.println(adminName);
//			System.out.println(adminPassword);
//			System.out.println(remember);

			admin.setAdminName(adminName);
			admin.setAdminPassword(adminPassword);

			if (admin.getAdminName() != null && !"".equals(admin.getAdminName()) && admin.getAdminPassword() != null
					&& !"".equals(admin.getAdminPassword())) {
				Admin admin_temp = adminService.login(admin);

				if (admin_temp != null) {
					HttpSession session = request.getSession();
					session.setAttribute("adminSession", admin_temp);

					if (remember != null && "remember".equals(remember)) {
						Cookie cookie = new Cookie("remember", adminName + "=" + adminPassword);
						cookie.setMaxAge(60 * 60 * 24 * 7);
						response.addCookie(cookie);
					} else {
						Cookie cookies[] = request.getCookies();
						for (Cookie cookie : cookies) {
							if ("remember".equals(cookie.getName())) {
								cookie.setMaxAge(0);
								response.addCookie(cookie);
							}
						}
					}
					response.sendRedirect(request.getContextPath() + "/back/index.jsp");
				} else {
					String message = "登录失败，用户名或密码错误！";
					message = URLEncoder.encode(message, "utf-8");
					response.sendRedirect(request.getContextPath() + "/back/login.jsp?message=" + message);
				}
			} else {
				String message = "登录失败，用户名或密码格式有误！";
				message = URLEncoder.encode(message, "utf-8");
				response.sendRedirect(request.getContextPath() + "/back/login.jsp?message=" + message);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void doAdminLogout(HttpServletRequest request, HttpServletResponse response) {
		try {
			HttpSession session = request.getSession();
			session.removeAttribute("adminSession");
			response.sendRedirect(request.getContextPath() + "/back/login.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
